package com.sy.core.exception;

import com.sy.core.util.RestResult;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 */
@ControllerAdvice
public class MExceptionHandler {

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public RestResult jsonExceptionHandler(HttpServletRequest request, AuthorizationException e) {
        return new RestResult(ErrorCode.SERVER_LOGIN_ERROR.getCode(),e.getMessage());
    }
    @ExceptionHandler(ParamsException.class)
    @ResponseBody
    public RestResult jsonExceptionHandler(HttpServletRequest request, ParamsException e) {
        return new RestResult(ErrorCode.SERVER_PARAMS_ERROR.getCode(),e.getMessage());
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public RestResult jsonExceptionHandler(HttpServletRequest request, UnauthorizedException e) {
        return new RestResult(ErrorCode.SERVER_NO_PERMISSIONS.getCode(),ErrorCode.SERVER_NO_PERMISSIONS.getMessage());
    }
}
